package com.jy.framework.sys.controller;

import com.jy.framework.annotation.RequiresAction;
import com.jy.framework.annotation.SysLog;
import com.jy.framework.base.BaseController;
import com.jy.framework.sys.entity.SysOnlineFileEntity;
import com.jy.framework.sys.service.SysOnlineFileService;
import com.jy.framework.utils.R;
import com.jy.framework.validator.ValidatorUtils;
import com.jy.web.file.service.impl.File2Url;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;


/**
 * 系统在线文件
 *
 * @author JuiceYan
 * @email JuiceYan@jy.com
 * @date 2019-05-02 10:11:25
 */
@Api(tags = "系统在线文件")
@RestController
@RequestMapping("sys/onlinefile")
public class SysOnlineFileController extends BaseController<SysOnlineFileService, SysOnlineFileEntity> {
    @Value("${jy.businessType}")
    private String businessType;

    /**
     * 保存
     */
    @ApiOperation("保存文件")
    @SysLog("保存功能")
    @PostMapping("/saveWithFile")
    @RequiresAction("save")
    public R saveWithFile(@RequestParam(value = "file",required = false) MultipartFile file, SysOnlineFileEntity entity) throws Exception {
        ValidatorUtils.validateEntity(entity);
        setUrl(file, entity);
        service.save(entity);
        return R.ok().put("entity", entity);
    }

    private void setUrl(MultipartFile file, SysOnlineFileEntity entity) throws Exception {
        if (null!=file&&!file.isEmpty()){
            String url = File2Url.transferFile2Url(file);
            entity.setUrl(url);
        }
    }

    /**
     * 更新
     *
     * @param entity
     * @return
     */
    @ApiOperation("修改文件")
    @SysLog("修改功能")
    @PostMapping("/updateWithFile")
    @RequiresAction("update")
    public R updateWithFile(@RequestParam(value = "file",required = false)MultipartFile file, SysOnlineFileEntity entity) throws Exception {
        ValidatorUtils.validateEntity(entity);
        setUrl(file, entity);
        service.updateById(entity);//全部更新
        return R.ok();
    }


}
